using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NPCManager : MonoBehaviour
{
    [Header("Cursor Texture")] 
    public Texture2D cursorTextureMagnifier;

    public Texture2D cursorTextureNormal;
    
    public static NPCManager instance;
    //负责生成销毁NPC，记录当前NPC

    public DocumentsManager documentManager;
    
    public List<GameObject> NPCforToday;
    public int NPCindex = 0;
    public GameObject currentNPC;
    public Transform NPCPosition;

    public bool isInAskMode;
    public GameObject askModePanel;
    public Image img_AskModeImage;
    public Sprite inAskModeSprite;
    public Sprite notInAskModeSprite;

    public GameObject NPCEnterDialogPrefab;
    public Transform NPCEnterDialogPosition;
    private GameObject thisEnterDialogBox;

    [Header("Askmode Related")] 
    public List<GameObject> clickablePartOnSmallTableProps;
    public RectTransform DialogBoxPosition;

    public int WrongDenyNPCNumber;
    public int CheckedNPCNumber;

    private void Awake()
    {
        if (instance == null)
        {
            DontDestroyOnLoad(gameObject);
            
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        Cursor.SetCursor(cursorTextureNormal, Vector2.zero, CursorMode.Auto);
        GenerateNPC();
    }

    public void GenerateNPC()
    {
        //生成新NPC
        currentNPC = Instantiate(NPCforToday[NPCindex], NPCPosition.position, Quaternion.identity,NPCPosition);
        //赋予其固定的位置
        currentNPC.transform.localPosition = new Vector3(0, 0, 0);
        //记录下一个需要生成的NPC
        NPCindex ++;
        //在屏幕上显示 'health code please'
        thisEnterDialogBox = Instantiate(NPCEnterDialogPrefab, NPCEnterDialogPosition.position, Quaternion.identity,NPCEnterDialogPosition);
        //在thisEnterDialogBox播放完成后，摧毁它
        Invoke("DestroyEnterDialogBox",4f);
        //把NPC放大后的手机图片，赋给PhoneMagnifier
        UIManager.instance.PhoneMagnified.GetComponent<Image>().sprite =
            currentNPC.GetComponent<NPCController>().phoneMagnified;

    }
    
    public void NPCApprove()
    {
        //TODO:判断是否放进来携带病毒的人
        currentNPC.GetComponent<Animator>().SetTrigger("NPCApprove");
        
    }

    public void NPCDeny()
    {
        if (currentNPC.GetComponent<NPCController>().CanAccess)
        {
            WrongDenyNPCNumber++;
        }
        currentNPC.GetComponent<Animator>().SetTrigger("NPCDeny");
        
    }

    //点击右下角黄色三角感叹号，进入询问模式，在询问模式下：
    //1, 无法拖拽任何可拖拽物品
    //2, 部分物品可以点击，进行询问（NPC生成的文件，默认的规则书，口罩 - 有没有？戴没戴？，手机）
    //3，询问有两种情况：
    //一，没有什么问题，则会弹出提示框，提示玩家没什么可问的，不会退出询问模式
    //二，确实有一些问题，则会弹出对话框，玩家和NPC展开对话，对话结束，退出询问模式
    public void EnterAskMode()
    {
        isInAskMode = true;
        //切换右下角问询Icon贴图&激活问询模式的遮罩
        img_AskModeImage.sprite = inAskModeSprite;
        askModePanel.SetActive(true);
        
        //如果当前NPC有文件放置于大桌子上，则激活其可点击部分组
        foreach (var document in currentNPC.GetComponent<NPCController>().documentsOnTheTable)
        {
            if (document.GetComponent<DocumentsOnTable>().isOnBigTable)
            {
                document.GetComponent<DocumentsOnTable>().AskModeClickablePartsGroup.SetActive(true);                
            }
        }
        
        //激活所有放置在小桌子上道具的可点击部分
        foreach (var clickablePart in clickablePartOnSmallTableProps)
        {
            clickablePart.SetActive(true);
        }

        //如果RuleBook在大桌子上，则激活当前页面对应的可点击部分组，同时关闭翻页功能
        if (RuleBookManager.instance.isOnBigTable)
        {
            RuleBookManager.instance.clickablePartsGroupOnRuleBook[RuleBookManager.instance.currentPage].SetActive(true);
            RuleBookManager.instance.turnPageLeft.interactable = false;
            RuleBookManager.instance.turnPageRight.interactable = false;
        }
        
        //让手机可以被询问
        currentNPC.GetComponent<NPCController>().phoneAskModeClickablePart.SetActive(true);
        
    }

    public void ExitAskMode()
    {
        isInAskMode = false;
        img_AskModeImage.sprite = notInAskModeSprite;
        askModePanel.SetActive(false);
        foreach (var document in currentNPC.GetComponent<NPCController>().documentsOnTheTable)
        {
            if (document.GetComponent<DocumentsOnTable>().isOnBigTable)
            {
                document.GetComponent<DocumentsOnTable>().AskModeClickablePartsGroup.SetActive(false);
            }
        }
        foreach (var clickablePart in clickablePartOnSmallTableProps)
        {
            clickablePart.SetActive(false);
        }
        
        if (RuleBookManager.instance.isOnBigTable)
        {
            RuleBookManager.instance.clickablePartsGroupOnRuleBook[RuleBookManager.instance.currentPage].SetActive(false);
            RuleBookManager.instance.turnPageLeft.interactable = true;
            RuleBookManager.instance.turnPageRight.interactable = true;
        }
        Cursor.SetCursor(NPCManager.instance.cursorTextureNormal, Vector2.zero, CursorMode.ForceSoftware);
        
        currentNPC.GetComponent<NPCController>().phoneAskModeClickablePart.SetActive(false);
    }

    public void AskModeSwitch()
    {
        if (isInAskMode)
        {
            ExitAskMode();
        }
        else
        {
            EnterAskMode();
        }
    }

    public void DestroyEnterDialogBox()
    {
        Destroy(thisEnterDialogBox);
    }

    public void GenerateNPCDialogBox(GameObject DialogBox)
    {
        Instantiate(DialogBox, DialogBoxPosition.position, Quaternion.identity, DialogBoxPosition.transform);
    }
}
